package sms.connect.manager.smpp;

import cn.hutool.core.thread.ThreadUtil;
import com.zx.sms.connect.manager.EndpointEntity;
import com.zx.sms.connect.manager.EndpointManager;
import com.zx.sms.connect.manager.smpp.SMPPClientEndpointEntity;
import com.zx.sms.connect.manager.smpp.SMPPServerChildEndpointEntity;
import com.zx.sms.connect.manager.smpp.SMPPServerEndpointEntity;
import com.zx.sms.handler.api.BusinessHandlerInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.locks.LockSupport;


/**
 *经测试，35个连接，每个连接每200/s条消息
 *lenovoX250能承担7000/s消息编码解析无压力。
 *10000/s的消息服务不稳定，开个网页，或者打开其它程序导致系统抖动，会有大量消息延迟 (超过500ms)
 *
 *低负载时消息编码解码可控制在10ms以内。
 *
 */


public class TestSMPPEndPoint {
	private static final Logger logger = LoggerFactory.getLogger(TestSMPPEndPoint.class);
	static private int THREAD_NUM = 2;
	private static ExecutorService executor = null;


	final static EndpointManager manager = EndpointManager.INS;

	private static Integer cnt=10000000;

	private static	String content=null;
	private static String sourceAddress=null;
	private static String destAddress=null;
	private static String ip="127.0.0.1";

	public static void main(String[] args) {
		if (args.length==0) {
			printUsage();
		}
		for (int i = 0; i <args.length; i++) {
			if (args[i].equalsIgnoreCase("-thread")){
				THREAD_NUM=Integer.valueOf(args[i+1]);
			}
			if (args[i].equalsIgnoreCase("-t")){
				cnt=Integer.valueOf(args[i+1]);
			}
			if (args[i].equalsIgnoreCase("-c")){
				content=args[i+1];
			}
			if (args[i].equalsIgnoreCase("-s")){
				sourceAddress=args[i+1];
			}
			if (args[i].equalsIgnoreCase("-d")){
				destAddress=args[i+1];
			}
			if (args[i].equalsIgnoreCase("-ip")){
				ip=args[i+1];
			}
		}

		for (int i=0;i<args.length;i++){
			if (args[i].equalsIgnoreCase("-server")){
				server();
			}
			if (args[i].equalsIgnoreCase("-client")){
				runTreads();
			}
		}
	}

	private static void runTreads() {
		executor = ThreadUtil.newExecutor(THREAD_NUM);
		logger.info("处理线程:{}个",THREAD_NUM);
		for (int i = 0; i<THREAD_NUM; i++){
			executor.execute(() -> {
				client();
			});
		}
	}


	private static void printUsage(){
		logger.info("使用说明:");
		logger.info("启动smpp server服务:java -jar smppSimulator-1.0-SNAPSHOT-jar-with-dependencies.jar  -server");
		logger.info("启动smpp client服务:java -jar smppSimulator-1.0-SNAPSHOT-jar-with-dependencies.jar  -client -ip 127.0.0.1 -t 100000  -thread 4 -c 你好 -s 10068 -d 18612345678");
		System.exit(0);
	}



	public static void server(){
		SMPPServerEndpointEntity server = new SMPPServerEndpointEntity();
		server.setId("smppserver");
		server.setHost("0.0.0.0");
		server.setPort(2776);
		server.setValid(true);
		//使用ssl加密数据流
		server.setUseSSL(false);

		SMPPServerChildEndpointEntity child = new SMPPServerChildEndpointEntity();
		child.setId("smppchild");
		child.setSystemId("901782");
		child.setPassword("ICP");

		child.setValid(true);
		child.setChannelType(EndpointEntity.ChannelType.DUPLEX);
		child.setMaxChannels((short)1000);
		child.setRetryWaitTimeSec((short)30);
		child.setMaxRetryCnt((short)0);
		child.setReSendFailMsg(false);
		child.setIdleTimeSec((short)15);
//		child.setWriteLimit(200);
//		child.setReadLimit(200);
		List<BusinessHandlerInterface> serverhandlers = new ArrayList<BusinessHandlerInterface>();
		serverhandlers.add(new SMPPMessageReceiveHandler());
		child.setBusinessHandlerSet(serverhandlers);
		server.addchild(child);
		manager.addEndpointEntity(server);
		manager.openEndpoint(server);
		logger.info("start SmppServer on 2776");
		LockSupport.park();
		EndpointManager.INS.close();
	}

	public static void client(){
		SMPPClientEndpointEntity client = new SMPPClientEndpointEntity();
		client.setId("smppclient");
		client.setHost(ip);
		client.setPort(2776);
		client.setSystemId("901782");
		client.setPassword("ICP");
		client.setChannelType(EndpointEntity.ChannelType.DUPLEX);

		client.setMaxChannels((short)10);
		client.setRetryWaitTimeSec((short)100);
		client.setUseSSL(false);
		client.setReSendFailMsg(false);
//		client.setWriteLimit(200);
//		client.setReadLimit(200);
		client.setSupportLongmsg(EndpointEntity.SupportLongMessage.SEND);  //接收长短信时不自动合并
		List<BusinessHandlerInterface> clienthandlers = new ArrayList<BusinessHandlerInterface>();
		clienthandlers.add( new SMPPSessionConnectedHandler(Thread.currentThread(),cnt,content,sourceAddress,destAddress));
		client.setBusinessHandlerSet(clienthandlers);


		manager.addEndpointEntity(client);
		manager.openEndpoint(client);
//		Thread.sleep(1000);
		logger.info("start client.....");
		LockSupport.park();
		EndpointManager.INS.close();
	}

}

